﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KhachSan.Process.Public;
using KhachSan.Process.Private.Tien;

namespace KhachSan.Process.Private.Bo
{
    public partial class F_Login : Form
    {
        public F_Login()
        {
            InitializeComponent();
        }

        private void F_Login_Load(object sender, EventArgs e)
        {
            this.txtNgay.Text = DateTime.Now.ToString();
            TinhCa();
        }

        private void TinhCa()
        {
            XLDuLieu.LoadCombobox("Select * from CaLamViec", "TenCa", "ID_CLV", cbCa);
            DataTable bangCa = (DataTable)cbCa.Properties.DataSource;
            int gioHienTai = DateTime.Now.Hour;
            int tu, den;
            for (int i = 0; i < bangCa.Rows.Count; i++)
            {
                tu = (int)bangCa.Rows[i]["TuGio"];
                den = (int)bangCa.Rows[i]["DenGio"];
                if (gioHienTai < den && gioHienTai >= tu)
                {
                    cbCa.Text = bangCa.Rows[i]["TenCa"].ToString();
                    break;
                }
            }
        }

        private void bnThoat_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void bnDangNhap_Click(object sender, EventArgs e)
        {
            try
            {
                string lenh = string.Format("sp_login N'{0}',N'{1}','{2}',{3}", txtTenDangNhap.Text, txtMatKhau.Text, txtNgay.Text, cbCa.EditValue);
                DataTable bang = XLDuLieu.LoadDataTable(lenh);
                if (bang != null)
                {
                    if (bang.Rows.Count > 0)
                    {
                        DataRow r = bang.Rows[0];
                        bool kichHoat = (bool)r["KichHoat"];
                        if (kichHoat == true)
                        {
                            lenh = string.Format("sp_LayDanhSachChucNang {0}", r["MaNhom"]);
                            DataTable ChucNang = XLDuLieu.LoadDataTable(lenh);

                            List<string> danhSachChucNang = new List<string>();
                            for (int i = 0; i < ChucNang.Rows.Count; i++)
                                danhSachChucNang.Add(ChucNang.Rows[i]["MaChucNang"].ToString());
                            ThongTinDangNhap thongTinDangNhap = new ThongTinDangNhap(
                                r["TenDangNhap"].ToString()
                                , r["MatKhau"].ToString()
                                , r["MatKhau2"].ToString()
                                , r["MaNV"].ToString()
                                , r["MaNhom"].ToString()
                                , (bool)r["KichHoat"]
                                , r["Ho"].ToString()
                                , r["Ten"].ToString()
                                , r["DiaChi"].ToString()
                                , r["SoDT"].ToString()
                                , r["Email"].ToString()
                                , r["MaBP"].ToString()
                                , r["MaKS"].ToString()
                                , danhSachChucNang
                                );
                            if (txtMatKhau.Text == r["MatKhau2"].ToString())
                            {
                                thongTinDangNhap.MuonMatKhau = true;
                                lenh = string.Format("update TaiKhoanDangNhap set MatKhau2=MatKhau where TenDangNhap=N'{0}'", r["TenDangNhap"]);
                                XLDuLieu.ExecuteNonQuery(lenh);
                            }
                            XLDuLieu.thongTinDangNhap = thongTinDangNhap;
                            F_Main f = new F_Main();
                            f.Show();
                            this.Hide();
                        }
                        else
                        {
                            lbThongBao.Text = "Tài khoản chưa kích hoạt\r\nLiên lạc quản lý để kích hoạt lại tài khoản";
                        }
                    }
                    else lbThongBao.Text = "Thông tin không hợp lệ";
                }
                else lbThongBao.Text = "Thông tin không hợp lệ";
            }
            catch { }
        }

        private void txtMatKhau_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                bnDangNhap.Focus();
                SendKeys.Send("{Enter}");
            }
        }
    }
}